package cn.tnar.cloud.userservice.controller;

import cn.tnar.cloud.userservice.feign.ParkInfoFeign;
import cn.tnar.cloud.userservice.model.entity.AcctMember;
import cn.tnar.cloud.userservice.model.service.AcctMemberService;
import cn.tnar.cloud.userservice.util.ResultJson;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


/**
 * @author dzx
 * @ClassName:
 * @Description:
 * @date 2019年04月09日 09:49:18
 */
@RestController
@RequestMapping("/member")
@Api(tags = "AcctMemberController")
public class AcctMemberController {

    @Autowired
    private AcctMemberService acctMemberService;

    @Autowired
    private ParkInfoFeign parkInfoFeign;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/index")
    @ApiOperation("用户index信息")
    public  String index(){
        return "用户服务";
    }

    @GetMapping("/getById")
    @ApiOperation("根据id查询用户")
    @HystrixCommand(fallbackMethod = "getByIdFallBack", threadPoolProperties = {
            @HystrixProperty(name = "coreSize", value = "10"), @HystrixProperty(name = "maxQueueSize", value = "100"),
            @HystrixProperty(name = "queueSizeRejectionThreshold", value = "20") }, commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000") })
    public ResultJson getById(@RequestParam(defaultValue = "131881") Long id){
        try {
            return ResultJson.getInstance().setCode(200).setData(acctMemberService.selectOneById(id)).setMsg("success");
        }catch (Exception e){
            return ResultJson.getInstance().setCode(400).setMsg("error");
        }
    }

    /**
     * getById熔断回调方法
     * @param id
     * @return
     */
    public  ResultJson getByIdFallBack(@RequestParam(defaultValue = "131881") Long id){
         AcctMember acctMember  = new AcctMember();
         acctMember.setId(00000L);
         System.out.println("服务熔断了");
         return ResultJson.getInstance().setCode(400).setMsg("error");
    }

    @GetMapping("/selectByPage")
    @ApiOperation("分页查询")
    public ResultJson selectByPage(int current,int size){
        IPage<AcctMember> iPage = acctMemberService.page(new Page<>(current, size), new QueryWrapper<AcctMember>().orderByDesc("id"));
        return ResultJson.getInstance().setCode(200).setData(iPage).setMsg("success");
    }



    @GetMapping("/parkIndexByRestTemplate")
    @ApiOperation("调用停车场服务-获取index信息")
    public  String parkIndexByRestTemplate(){
       return  restTemplate.getForObject("http://park-service/park/index", String.class);
    }

    @GetMapping("/parkIndex")
    @ApiOperation("调用停车场服务-获取index信息")
    public  String  parkIndex(){
        return parkInfoFeign.index();
    }

    @GetMapping("/getParkInfoById")
    @ApiOperation("调用停车场服务-根据id查询停车场信息")
    public ResultJson getParkInfoById(@RequestParam(defaultValue = "123742") Long id){
        return parkInfoFeign.getById(id);
    }



}
